import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class sql {
    private static final String URL = "jdbc:mysql://localhost:3306/jiying110";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    public static boolean authenticateUser(String username, String password, String role) {
        String query = "SELECT * FROM users WHERE username=? AND password=? AND role=?";

        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            preparedStatement.setString(3, role);

            ResultSet resultSet = preparedStatement.executeQuery();
            return resultSet.next(); // 如果查询结果存在，说明验证成功
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
